package com.kawins.supers.http;

import com.alibaba.fastjson.JSON;

/**
 * @author yanwe
 * createTime 2017-12-2017/12/8 10:41
 */
public class ApiResult {

    private String msg;

    private Integer ret;

    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getRet() {
        return ret;
    }

    public void setRet(Integer ret) {
        this.ret = ret;
    }

    public String getData() {
        return data;
    }

    public <T> T getData(Class<T> clazz) {
        if (data == null){
            return null;
        }
        return JSON.parseObject(data,clazz);
    }

    public void setData(String data) {
        this.data = data;
    }

    public boolean isNull(){
        return msg == null && ret == null && data == null;
    }

    public static final class ApiResultBuilder {
        private String msg;
        private Integer ret;
        private String data;

        private ApiResultBuilder() {
        }

        public static ApiResultBuilder anApiResult() {
            return new ApiResultBuilder();
        }

        public ApiResultBuilder withMsg(String msg) {
            this.msg = msg;
            return this;
        }

        public ApiResultBuilder withRet(Integer ret) {
            this.ret = ret;
            return this;
        }

        public ApiResultBuilder withData(String data) {
            this.data = data;
            return this;
        }

        public ApiResult build() {
            ApiResult apiResult = new ApiResult();
            apiResult.setMsg(msg);
            apiResult.setRet(ret);
            apiResult.setData(data);
            return apiResult;
        }
    }

    @Override
    public String toString() {
        return "ApiResult{" +
                "msg='" + msg + '\'' +
                ", ret=" + ret +
                ", data='" + data + '\'' +
                '}';
    }
}
